package com.chain.BrotherExchange.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.UserPayment;
import com.chain.BrotherExchange.event.SubmitAlipayEvent;
import com.chain.BrotherExchange.event.SubmitBankCardEvent;
import com.chain.BrotherExchange.event.SubmitPayPalEvent;
import com.chain.BrotherExchange.event.SubmitWeChatPayEvent;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateActivity;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.viewmodel.PaymentViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;

/**
 * 支付方式
 */
public class PaymentActivity extends AbsNetworkStateActivity {

    @Bind(R.id.upcash_payment_state_text) TextView upcashPaymentStateText;
    @Bind(R.id.upcash_payment_layout) LinearLayout upcashPaymentLayout;
    @Bind(R.id.ali_payment_state_text) TextView aliPaymentStateText;
    @Bind(R.id.ali_payment_layout) LinearLayout aliPaymentLayout;
    @Bind(R.id.we_chat_payment_state_text) TextView weChatPaymentStateText;
    @Bind(R.id.we_chat_payment_layout) LinearLayout weChatPaymentLayout;
    @Bind(R.id.pay_pal_state_text) TextView payPalStateText;
    @Bind(R.id.pay_pal_layout) LinearLayout payPalLayout;

    private PaymentViewModel mPaymentViewModel;
    private UserPayment.DataBean.SetpaysBean mSetpays;

    public static void launch(Context context) {
        context.startActivity(new Intent(context, PaymentActivity.class));
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    public int getTitleLayoutRes() {
        return R.layout.layout_default_title_bar;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_payment;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.title_payment);

        // 视图
        mPaymentViewModel = ViewModelProviders.of(this).get(PaymentViewModel.class);

        // 初始化用户支付方式
        initUserPayment();
    }

    private void initUserPayment() {
        mPaymentViewModel.userPayment().observe(this, resource -> {
            if (resource.state == Resource.Status.SUCCESS) {
                loadUserPaymentOntoVie(resource.source.getSetpays());
            }
        });
    }

    private void loadUserPaymentOntoVie(UserPayment.DataBean.SetpaysBean setpays) {
        mSetpays = setpays;

        // 银行卡
        if (mSetpays.getBank_pay() == C.API_SOUDN_PAYMENT) {
            upcashPaymentStateText.setText(R.string.modify);
            upcashPaymentLayout.setOnClickListener(v ->
                    AddBankCardActivity.launch(v.getContext(), C.VALUE_MODIFY));
        } else {
            upcashPaymentStateText.setText(R.string.setting);
            upcashPaymentLayout.setOnClickListener(v ->
                    AddBankCardActivity.launch(v.getContext(), C.VALUE_SETTING));
        }

        // 支付宝
        if (mSetpays.getAli_pay() == C.API_SOUDN_PAYMENT) {
            aliPaymentStateText.setText(R.string.modify);
            aliPaymentLayout.setOnClickListener(v ->
                    AddWeChatPayOrAliPayActivity.launch(v.getContext(), C.VALUE_ALI_PAY, C.VALUE_MODIFY)
            );
        } else {
            aliPaymentStateText.setText(R.string.setting);
            aliPaymentLayout.setOnClickListener(v ->
                    AddWeChatPayOrAliPayActivity.launch(v.getContext(), C.VALUE_ALI_PAY, C.VALUE_SETTING)
            );
        }

        // 微信
        if (mSetpays.getWechat_pay() == C.API_SOUDN_PAYMENT) {
            weChatPaymentStateText.setText(R.string.modify);
            weChatPaymentLayout.setOnClickListener(v ->
                    AddWeChatPayOrAliPayActivity.launch(v.getContext(), C.VALUE_WE_CHAT_PAY, C.VALUE_MODIFY)
            );
        } else {
            weChatPaymentStateText.setText(R.string.setting);
            weChatPaymentLayout.setOnClickListener(v ->
                    AddWeChatPayOrAliPayActivity.launch(v.getContext(), C.VALUE_WE_CHAT_PAY, C.VALUE_SETTING)
            );
        }

        // payPal
        if (mSetpays.getPaypal_pay() == C.API_SOUDN_PAYMENT) {
            payPalStateText.setText(R.string.modify);
            payPalLayout.setOnClickListener(v ->
                    AddPayPalActivity.launch(v.getContext(), C.VALUE_MODIFY));
        } else {
            payPalStateText.setText(R.string.setting);
            payPalLayout.setOnClickListener(v ->
                    AddPayPalActivity.launch(v.getContext(), C.VALUE_SETTING));
        }
    }

    @Override
    public void onAnewRequest() {
        initUserPayment();
    }

    @Subscribe
    public void addBankCardEvent(SubmitBankCardEvent event){
        if (mSetpays != null) {
            mSetpays.setBank_pay(1);
            loadUserPaymentOntoVie(mSetpays);
        }
    }

    @Subscribe
    public void addPayPalEvent(SubmitPayPalEvent event){
        if (mSetpays != null) {
            mSetpays.setPaypal_pay(1);
            loadUserPaymentOntoVie(mSetpays);
        }
    }

    @Subscribe
    public void addAliPayEvent(SubmitAlipayEvent event){
        if (mSetpays != null) {
            mSetpays.setAli_pay(1);
            loadUserPaymentOntoVie(mSetpays);
        }
    }

    @Subscribe
    public void addWeChatPay(SubmitWeChatPayEvent event){
        if (mSetpays != null) {
            mSetpays.setWechat_pay(1);
            loadUserPaymentOntoVie(mSetpays);
        }
    }
}
